package com.cloudspace.jindun.locate;

import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.CommonResult;
import com.cloudspace.jindun.model.LocateAreaResult;
import com.cloudspace.jindun.network.HttpManager;
import com.cloudspace.jindun.network.api.BaseAPI;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.network.base.GsonRequest;
import com.cloudspace.jindun.network.base.RequestFactory;
import com.cloudspace.jindun.user.UserSessionManager;

/**
 * Created by zengxianhua on 17/3/2.
 */

public class LocateApi extends BaseAPI{
    private StringBuilder getBaseLocateRequestUrl(){
        return new StringBuilder(String.format("http://%s/jindun/%s/locate/",
                Configs.SERVER_IP,Configs.API_VERSION));
    }

    /**
     * 授权码检测
     * @param tag
     * @param code
     * @param callback
     */
    public void checkCode(String tag, String code, ApiCallback<LocateAreaResult> callback){
        String url = getBaseLocateRequestUrl() + "checkCode";

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, LocateAreaResult.class, callback)
                .setParams("code", code)
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

    /**
     * 上报经纬度坐标
     * @param tag
     * @param longitude
     * @param latitude
     * @param callback
     */
    public void reportCoordinates(String tag, double longitude, double latitude, ApiCallback<CommonResult> callback){
        String url = getBaseLocateRequestUrl() + "reportCoordinates";

        GsonRequest request = RequestFactory.newInstance()
                .createPost(url, CommonResult.class, callback)
                .setParams("userX", longitude)
                .setParams("userY", latitude)
                .setParams("userId", UserSessionManager.getInstance().getUserId())
                .setNoCache();

        HttpManager.addRequest(request, tag);
    }

}
